<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px; width: 100%"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      @click.native="doAdd('')"
                    >添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 树状图 -->
        <el-tree :data="list" :default-expand-all="true">
          <template #default="{ data }">
            <!-- 用一个行列布局 -->
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <!-- 把当前子部门的id传给点击事件 -->
                        <el-dropdown-item
                          @click.native="doAdd(data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="doEdit(data.id)"
                        >编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          v-if="data.children && data.children.length === 0"
                          @click.native="doDel(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>

      <!-- 实现新增子部门或者编辑部门的弹层框 -->
      <el-dialog
        :title="isEdit ? '编辑部门' : '新增部门'"
        :visible.sync="showDialog"
        :close-on-click-modal="false"
        @close="resForm"
      >
        <!-- 在弹层中放入子组件 -->
        <!-- 通过自定义属性父传子把id传到子组件中 -->
        <!-- 因为created钩子只能触发一次,需要通过销毁创新子组件来触发钩子进行数据更新 -->
        <deptDialog
          v-if="showDialog"
          :id="curId"
          ref="adddeptDialog"
          :is-edit="isEdit"
          :origin-list="originList"
          @success="hSuccess"
          @close="showDialog = false"
        />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { tranDataToTree } from '@/utils'
import deptDialog from './deptDialog.vue'

export default {
  components: { deptDialog },
  data() {
    return {
      list: [],
      showDialog: false, // 控制弹层的显示隐藏
      curId: '', // 存储当前部门id
      isEdit: false, // 默认false,为新增部门弹层,为true的时候,是编辑部门弹层
      originList: []
    }
  },
  created() {
    // 在数据初始化钩子中触发方法发送ajax
    this.loadDepartments()
  },
  methods: {
    // 获取组织架构信息
    async loadDepartments() {
      try {
        // 调用api封装的ajax方法发送ajax
        const res = await getDepartments()
        console.log(res, 'getDepartments')

        // 筛选出需要的数据传入子组件进行表单校验
        this.originList = res.data.depts.map(item => {
          return { id: item.id, pid: item.pid, name: item.name, code: item.code }
        })

        // 删除数组中的第一条数据(公司名称)
        res.data.depts.shift()

        // 调取封装函数,将平铺的数据转化成树状数据
        // 将data中depts的数据存储到list中
        this.list = tranDataToTree(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    },
    // 新增
    doAdd(id) {
      this.curId = id // 把接收过来的id存到data中
      this.showDialog = true // 显示弹层
      this.isEdit = false
    },
    // 修改
    doEdit(id) {
      this.curId = id
      this.isEdit = true
      this.showDialog = true
      // 解决数据不更新的第二种方法
      // this.$nextTick(() => {
      //   this.$refs.adddeptDialog.loadDepartmentDetail()
      // })
    },
    // 删除
    doDel(id) {
      this.$confirm('是否删除该部门?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          // 调用函数发送删除请求
          const res = await delDepartment(id)
          if (!res.success) return this.$message.error(res.message)
          this.$message.success(res.message)
          this.loadDepartments()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    hSuccess() {
      // 子传父自定义事件
      // 重新渲染数据
      this.loadDepartments()
      // 关闭弹层
      this.showDialog = false
    },
    resForm() {
      // this.$refs.adddeptDialog.resetForm()
      console.log('解决数据不更新的方法3', '父组件')
    }
  }
}
</script>
